//
//  NSString+Date.m
//  TestNSStringAndNSDate
//
//  Created by 计海峰 on 16/5/6.
//  Copyright © 2016年 好牛. All rights reserved.
//

#import "NSString+Date.h"
#import "NSDate+Utilities.h"

#define DATE_COMPONENTS (NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit)
#define CURRENT_CALENDAR [NSCalendar currentCalendar]


@implementation NSString (Date)
/**
 *  将yyyy-MM-dd 或者yyyy-MM-dd HH:mm:ss格式字符串改成NSDate
 *
 *  @return NSDate
 */
-(NSDate *)convertToDate{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    if (self.length > 11) {
        [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
    }else{
        [dateFormatter setDateFormat: @"yyyy-MM-dd"];
    }
    NSDate *destDate= [dateFormatter dateFromString:self];
    return destDate;
}

/**
 *  将yyyy-MM-dd 或者yyyy-MM-dd HH:mm:ss格式字符串改成格式化的时间表示方式
 *
 *  @return NSString
 */
-(NSString *)convertToFormatString{
  return [self convertTimeIntervalToDateString:[[self convertToDate] timeIntervalSince1970]];
}


-(NSString *)convertTimeIntervalToDateString:(int)minDate
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateLoca = [NSString stringWithFormat:@"%d",minDate];
    NSTimeInterval time=[dateLoca doubleValue];
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    NSString *detailString = [formatter stringFromDate:detaildate];
    //一天内
    if ([detaildate isToday]){
        return  [[detailString stringByReplacingOccurrencesOfString:@" " withString:@""] substringFromIndex:10];
    }
    if ([detaildate isYesterday]) {
        //是否是昨天的
        return [NSString stringWithFormat:@"昨天%@", [detailString substringFromIndex:10]];
    }
    if ([detaildate isAnteayerday]){
        //是否是前天的
        return [NSString stringWithFormat:@"前天%@", [detailString substringFromIndex:10]];
    }
    return [formatter stringFromDate:detaildate];
}


@end
